<template>
  <a-modal
    :visible="visible"
    title="使用申请"
    cancelText="取消"
    okText="确定"
    @ok="submit"
    @cancel="cancel"
  >
    <a-form
       ref="formRef"
      :model="formState"
      :rules="formRules"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
    >
      <a-form-item ref="goodsname" label="物品名称" name="goodsname">
        <a-input v-model:value="formState.goodsname" disabled="true" />
      </a-form-item>
      <a-form-item ref="goodssum" label="物品总数" name="goodssum">
        <a-input v-model:value="formState.goodssum"  disabled="true"/>
      </a-form-item>
      <a-form-item ref="number" label="剩余可用数量" name="number">
        <a-input v-model:value="formState.number"  disabled="true"/>
      </a-form-item>
      <a-form-item ref="applicationNumber" label="需申请数量" name="applicationNumber">
        <a-input type="number" v-model:value="formState.applicationNumber" />
      </a-form-item>
      <a-form-item label="申请用途" name="remark">
        <a-textarea v-model:value="formState.remark" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import {defineComponent, reactive, ref, toRaw, watch} from "vue";
import {message} from "_ant-design-vue@2.2.8@ant-design-vue";
import {apply} from "@/api/module/application";
export default defineComponent({
  props: {
    visible: {
      type: Boolean,
    },
    record: {
      type: Object,
    }
  },
  emit: ["close"],
  setup(props, context) {

    const formRef = ref();

    const formState = reactive({});

    watch(props, (props) => {
        formState.id = props.record.id
        formState.goodsname = props.record.goodsname
        formState.number = props.record.number
        formState.goodssum = props.record.goodssum
    })

    const formRules = { };

    const submit = (e) => {
      apply(toRaw(formState)).then((response)=>{
        if(response.success){
          message.success({ content: '申请成功，请等待审核' , duration: 1 }).then(()=>{
            cancel();
          });
        }else{
          message.error({ content: response.msg, duration: 1 }).then(()=>{
            cancel();
          });
        }
      });
    };

    const cancel = (e) => {
      formRef.value.resetFields();
      context.emit("close", false);
    };

    return {
      submit,
      cancel,
      formRef,
      formState,
      formRules,

      labelCol: { span: 6 },
      wrapperCol: { span: 18 },
    };
  },
});
</script>
